import json
from enum import Enum


class MType(Enum):
    newPeer = 0
    peers = 1
    introduce = 2
    input = 3
    exit = 4


class Packet:
    @classmethod
    def pack(cls, mes: MType = None, res=None):
        return json.dumps({
            't': mes.value,
            'd': res
        })

    @classmethod
    def unpack(cls, data):
        tmp = json.loads(data)
        return {
            't': MType(tmp.get('t')),
            'd': tmp.get('d'),
        }


if __name__ == '__main__':
    packed = Packet.pack(MType.newPeer, '')
    print(packed, type(packed))
    unpacked = Packet.unpack(packed)
    print(unpacked, type(unpacked))
